package com.ktgj.customer.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.customer.domain.TravelGroupNumberDto;
import com.ktgj.customer.domain.vo.GroupNumVo;
import com.ktgj.customer.domain.vo.SetOffTimeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.TravelGroupNumber;
import com.ktgj.customer.service.ITravelGroupNumberService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 旅游团号Controller
 * 
 * @author ruoyi
 * @date 2021-10-26
 */
@ControllerAdvice
@RestController
@RequestMapping("/number")
public class TravelGroupNumberController extends BaseController
{
    @Autowired
    private ITravelGroupNumberService travelGroupNumberService;

    /**
     * 查询旅游团号列表
     */
    //@PreAuthorize(hasPermi = "customer:number:list")
    @GetMapping("/list")
    public TableDataInfo list(TravelGroupNumberDto travelGroupNumberDto) {
        startPage();
        List<TravelGroupNumberDto> list = travelGroupNumberService.selectTravelGroupNumberList(travelGroupNumberDto);
        return getDataTable(list);
    }

    /**
     * 导出旅游团号列表
     */
    //@PreAuthorize(hasPermi = "customer:number:export")
    @Log(title = "旅游团号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TravelGroupNumberDto travelGroupNumberDto) throws IOException {
        List<TravelGroupNumberDto> list = travelGroupNumberService.selectTravelGroupNumberList(travelGroupNumberDto);
        ExcelUtil<TravelGroupNumberDto> util = new ExcelUtil<TravelGroupNumberDto>(TravelGroupNumberDto.class);
        util.exportExcel(response, list, "旅游团号数据");
    }

    /**
     * 获取旅游团号详细信息
     */
    //@PreAuthorize(hasPermi = "customer:number:query")
    @GetMapping(value = "/{numberId}")
    public AjaxResult getInfo(@PathVariable("numberId") Long numberId) {
        return AjaxResult.success(travelGroupNumberService.selectTravelGroupNumberById(numberId));
    }

    /**
     * 新增旅游团号
     */
    //@PreAuthorize(hasPermi = "customer:number:add")
    @Log(title = "旅游团号", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TravelGroupNumberDto travelGroupNumberDto) {
        return toAjax(travelGroupNumberService.insertTravelGroupNumber(travelGroupNumberDto));
    }

    /**
     * 修改旅游团号
     */
    //@PreAuthorize(hasPermi = "customer:number:edit")
    @Log(title = "旅游团号", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TravelGroupNumberDto travelGroupNumberDto) {
        return toAjax(travelGroupNumberService.updateTravelGroupNumber(travelGroupNumberDto));
    }

    /**
     * 删除旅游团号
     */
    //@PreAuthorize(hasPermi = "customer:number:remove")
    @Log(title = "旅游团号", businessType = BusinessType.DELETE)
	@DeleteMapping("/{numberIds}")
    public AjaxResult remove(@PathVariable Long[] numberIds) {
        return toAjax(travelGroupNumberService.deleteTravelGroupNumberByIds(numberIds));
    }

    /**
     * 查询出发时间详情
     * */
    @PostMapping("/setOffTimeVoList")
    public AjaxResult setOffTimeVoList(@RequestBody Map<String, Long> map)
    {
        Long groupId = map.get("groupId");
        List<SetOffTimeVo> setOffTimeVoList = travelGroupNumberService.setOffTimeVoList(groupId);
        return AjaxResult.success(setOffTimeVoList);
    }

    /**
     * 团号列表
     * */
    @PostMapping("/groupNumList")
    public TableDataInfo groupNumList(@RequestBody GroupNumVo groupNumVo){
        startPage();
        List<TravelGroupNumber> travelGroupNumbers;
        try {
            travelGroupNumbers = travelGroupNumberService.groupNumList(groupNumVo);
        } catch (Exception e){
            List<TravelGroupNumber> travelGroupNumberList = new ArrayList<>();
            TravelGroupNumber travelGroupNumber = new TravelGroupNumber();
            travelGroupNumber.setMsg(e.getMessage());
            travelGroupNumberList.add(travelGroupNumber);
            return getDataTable(travelGroupNumberList);
        }
        return getDataTable(travelGroupNumbers);
    }

    /**
     * 新增团号
     * */
    @PostMapping("/insertGroupNum")
    public AjaxResult insertGroupNum(HttpServletRequest request, @RequestBody TravelGroupNumber travelGroupNumber){
        int i;
        try {
            i = travelGroupNumberService.insertGroupNum(request, travelGroupNumber);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (i > 0){
            return AjaxResult.success("新增成功");
        }
        else {
            return AjaxResult.error("新增失败");
        }
    }

    /**
     * 查询待修改的团号
     * */
    @PostMapping("/selGroupNum")
    public AjaxResult selGroupNum(@RequestBody Map<String, Object> map){
        Long id = Long.valueOf(map.get("id").toString());
        TravelGroupNumber travelGroupNumber = travelGroupNumberService.selGroupNum(id);
        return AjaxResult.success(travelGroupNumber);
    }

    /**
     * 修改团号
     * */
    @PostMapping("/updateNum")
    public AjaxResult updateNum(HttpServletRequest request, @RequestBody TravelGroupNumber travelGroupNumber){
        int i;
        try {
            i = travelGroupNumberService.updateNum(request, travelGroupNumber);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (i > 0){
            return AjaxResult.success("新增成功");
        }
        else {
            return AjaxResult.error("新增失败");
        }
    }

    /**
     * 团号上下架操作
     * */
    @PostMapping("/operate")
    public AjaxResult operate(@RequestBody Map<String, Object> map){
        Long type = Long.valueOf(map.get("type").toString());
        Long id = 0l;
        if (map.get("id") != null &&!map.get("id").equals("")){
            id = Long.valueOf(map.get("id").toString());
        }

        int i = travelGroupNumberService.operate(type, id);
        if (i > 0){
            return AjaxResult.success("成功");
        }
        else {
            return AjaxResult.error("失败");
        }
    }
}
